package com.dongjiantong.sys.serviceImpl;


import com.alibaba.dubbo.config.annotation.Service;

import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.common.redis.RedisUtil;
import com.dongjiantong.common.utils.StringUtil;
import com.dongjiantong.mybatis.ServiceMybatis;
import com.dongjiantong.sys.mapper.DicUnitMapper;
import com.dongjiantong.sys.model.DicUnit;
import com.dongjiantong.sys.service.AnimalTypeService;
import com.dongjiantong.sys.service.DicUnitService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by cao on 2017/10/18.
 *
 * @author muzhong
 */
@Slf4j
@Service(version = "1.0.0")
public class DicUnitServiceImpl extends ServiceMybatis<DicUnit> implements DicUnitService {

    public static final String DicUnit_KEY = "DicUnit:";

    @Resource
    private DicUnitMapper dicUnitMapper;

    @Autowired
    private AnimalTypeService animalTypeService;


    @Override
    public DicUnit queryUnitByCode(Integer code) {
        String key = DicUnit_KEY + String.format("getUnitByCode.%d", code);
        DicUnit dicUnit = RedisUtil.getObject(key, DicUnit.class);

        if (dicUnit != null) {
            return dicUnit;
        } else {
//            dicUnit = new DicUnit();
//            dicUnit.setCode(code);

            Example example = new Example(DicUnit.class);

            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("code", code);


            dicUnit = this.selectOne(example);


            if (dicUnit != null) {

                RedisUtil.setObject(key, dicUnit);
            }

            return dicUnit;
        }

    }

    @Override
    public DicUnit queryUnitByName(String name) {
        String key = DicUnit_KEY + String.format("getUnitByName.%d", name);
        DicUnit dicUnit = RedisUtil.getObject(key, DicUnit.class);

        if (dicUnit != null) {
            return dicUnit;
        } else {
            Example example = new Example(DicUnit.class);

            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("unit", name);

            dicUnit = this.selectOne(example);

            if (dicUnit != null) {

                RedisUtil.setObject(key, dicUnit);
            }

            return dicUnit;
        }

    }

    @Override
    public List<DicUnit> queryListByAnimalType(Integer animalType) {
        String key = DicUnit_KEY + String.format("getListByAnimalType.%d", animalType);

        List<DicUnit> list = RedisUtil.getList(key);

        if (list != null && list.size() > 0) {
            return list;
        }

        Example example = new Example(DicUnit.class);

        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("animalType", animalType);


        list = dicUnitMapper.selectByExample(example);
        RedisUtil.setList(key, list);

        return list;


    }

    @Override
    public PageInfo<DicUnit> getAllList(Integer pageNum, Integer pageSize, Integer animalType, Integer code) {

        DicUnit dicUnit = new DicUnit();
        dicUnit.setAnimalType(animalType);
        dicUnit.setCode(code);
        PageHelper.startPage(pageNum, pageSize);
        List<DicUnit> dicUnitList = dicUnitMapper.select(dicUnit);
        return new PageInfo<>(dicUnitList);
    }

    @Override
    public Response<Boolean> saveUnit(DicUnit dicUnit) {

        DicUnit dicUnit1 = queryUnitByCode(dicUnit.getCode());

        if (dicUnit1 != null && !StringUtil.isBlank(dicUnit1.getCode())) {
            return Response.fail(HttpCode.BAD_REQUEST, "code已经存在");
        }

        dicUnit1 = queryUnitByName(dicUnit.getUnit());
        if (dicUnit1 != null && !StringUtil.isBlank(dicUnit1.getUnit())) {
            return Response.fail(HttpCode.BAD_REQUEST, "单位名称已经存在");
        }

        int Result = dicUnitMapper.insert(dicUnit);


        return Response.ok(Result > 0);
    }

}
